<?php

require_once("libs/controller.php");
require_once 'modelos/articulo.php';
require_once 'modelos/ubicaciones_articulo.php';
require_once 'modelos/cotizacion.php';
require_once 'modelos/comentarios.php';
require_once 'modelos/fotos_cotizacion.php';

/**
 * Class with all function for a purchaser.
 * Every xMethod allways receives two aditional parameters:
 * - id        : User id who makes the request
 * - lang      : Lang of user device
 * - user_type : User type (V / C / G)
 *   
 * @author Gonfre
 */
class PurchaserController extends Controller {
	
	public function __construct() {
	}
	
	
	public function index() {
	}


	/**
	 * Requests a price for a product
	 * @param description
	 * @param category
	 * @param locations
	 * @param only_photo
	 */
	public function xRequestPrice() {
		extract($_REQUEST);
		
		$a = new Articulo();
		
		$a->getConnection()->iniciarTransaccion();
		
		$a->setValue("descripcion", $description);
		$a->setValue("id_categoria", $category);
		$a->setValue("id_comprador", $id);
		$a->setValue("con_foto", $only_photo);
		
		if ($a->doSave()) {
			
			$response = $this->createResponseHeader(0, $lang);
			
			//get the saved request ID
			$i = $a->getLastSavedId();
			
			//insert locations
			$u = new Ubicaciones_articulo();
			$arr_l = explode(",", $locations);
			foreach ($arr_l as $location) {
				$u->clear();
				$u->setValue("id_articulo", $i);
				$u->setValue("id_ubicacion", $location);
				if (!$u->doSave()) {
					$this->returnKO(-8, $lang, $a->getConnection());
				}
			}
			
			$response = $response + array (
					"body" => array (
							"id_request" => $i
					)
			);
			
			
			$a->getConnection()->confirmarTransaccion();
			$this->putJsonResponse($response);
			
		} else {
			$this->returnKO(-8, $lang);
		}	
	}
	
	
	/**
	 * Get a received prices list (filtered)
	 * @param id_request
	 * @param with_photo
	 * @param price_min
	 * @param price_max
	 * @param locations
	 */
	public function xGetReceivedPriceList() {
		extract($_REQUEST);
		
		$c = new Cotizacion();
		
		if ($c->getReceivedPriceList($id, $id_request, $price_min, $price_max, $locations, $with_photo)) {
			
			$response = $this->createResponseHeader(0, $lang);

			$list = array();
			while ($c->next()) {
				//get last comment
				$last_comment = null;
				$comment_source = null;
				$co = new Comentarios();
				$co->addCondition("id_cotizacion", $c->getValue("id"));
				if ($co->doSelectMaxRecord("id")) {
					$last_comment = $co->getValue("texto");
					$comment_source = $co->getValue("origen");
				}
				
				array_push($list, array(
					"id" => $c->getValue("id"),
					"description" => $c->getValue("descripcion"),
					"price" => $c->getValue("precio"),
					"seller" => $c->getValue("nombre"),
					"location" => $c->getValue("id_ubicacion"),
					"seller_type" => $c->getValue("tipo_cuenta"),
					"photo_quantity" => $c->getValue("fotos"),
					"last_comment" => $last_comment,
					"comment_source" => $comment_source
				));
			}
				
			if (count($list) == 0) {
				$this->returnKO(2, $lang);
			}
				
			$response = $response + array (
					"body" => array (
							"price_list" => $list
					)
			);
				
			$this->putJsonResponse($response);
				
		} else {
			$this->returnKO(-8, $lang);
		}
		
	}
	
	
	/**
	 * Get details for a price
	 * @param id_price
	 */
	public function xGetPriceDetails() {
		extract($_REQUEST);
		
		$c = new Cotizacion();
		$c->setValue("id", $id_price);
		if ($c->doSelectAllWithForeign("usuario", "id_vendedor") && $c->next()) {
			
			$response = $this->createResponseHeader(0, $lang);
				
			//get photos
			$f = new Fotos_cotizacion();
			$photo_list = array();
			if ($f->doSelectAllWithCondition("id_cotizacion", $id_price)) {
				while ($f->next()) {
					array_push($photo_list, array(
						"id_photo" => $f->getValue("id_foto")
					));
				}
			}
			
			//get comments
			$co = new Comentarios();
			$co->addOrderBy("id", DB_DESC);
			$comment_list = array();
			if ($co->doSelectAllWithCondition("id_cotizacion", $id_price)) {
				while ($co->next()) {
					array_push($comment_list, array(
						"datetime" => $co->getValue("fecha"),
						"text" => $co->getValue("texto"),
						"source" => $co->getValue("origen"),
						"id_photo" => $co->getValue("id_foto")
					));
				}
			}
			
			//get Request desciption
			$a = $c->getForeign("articulo", "id_articulo");
			
			//build response
			$response = $response + array (
					"body" => array (
							"description" => $a->getValue("descripcion"),
							"price" => $c->getValue("precio"),
							"id_seller" => $c->getValue("id_vendedor"),
							"seller" => $c->getValue("nombre"),
							"seller_type" => $c->getValue("tipo_cuenta"),
							"photo_count" => count($photo_list),
							"photo_list" => $photo_list,
							"comment_count" => count($comment_list),
							"comment_list" => $comment_list,
					)
			);
				
			$this->putJsonResponse($response);
				
		} else {
			$this->returnKO(2, $lang);
		}
	}
	
	
	/**
	 * Agrees a purchase between purchaser and seller
	 * @param id_price
	 */
	public function xAgreePurchase() {
		extract($_REQUEST);
		
		
	}
}

?>